package com.wbao.dianniu.ui;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.umeng.analytics.MobclickAgent;
import com.wbao.dianniu.customView.CustomDatePicker;
import com.wbao.dianniu.customView.LoadingDialog;
import com.wbao.dianniu.data.WorkExperienceData;
import com.wbao.dianniu.listener.IJobWorkAddListener;
import com.wbao.dianniu.logical.GlobalContext;
import com.wbao.dianniu.manager.JobWorkAddManager;
import com.wbao.dianniu.manager.WorkListChangeManager;
import com.wbao.dianniu.utils.Notification;
import com.wbao.dianniu.utils.Utils;
import com.wbao.dianniu.R;

/**
 * 添加工作经历
 * 
 * @author yejianjun
 * 
 */
public class AddJobExActivity extends BaseActivity implements IJobWorkAddListener{

	private RelativeLayout rl_partIn;
	private RelativeLayout rl_deTime;
	private EditText et_post;
	private EditText et_desc;
	private TextView tv_partIn;
	private TextView tv_deTime;
	private Button saveBtn;
	private CustomDatePicker customDatePicker1, customDatePicker2;
	private JobWorkAddManager manager;
	// 等待框 控制多次点击
	private boolean isClick = false;
	private Dialog pDialog;
	private Integer id = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentLayout(R.layout.add_job_experience);
		setTitleName(getResources().getString(R.string.edit_job_experience));
		init();
		initDatePicker();
		initData();
		manager = new JobWorkAddManager(this);
		manager.addJobWorkAddListener(this);
	}

	private void initData(){
		Intent intent = getIntent();
		Bundle bundle = intent.getBundleExtra(GlobalContext.BUNDLE_EDIT);
		if(null != bundle){
			id = bundle.getInt(GlobalContext.BUNDLE_ID,0);
			tv_partIn.setText(bundle.getString(GlobalContext.BUNDLE_STARTDATE, ""));
			tv_deTime.setText(bundle.getString(GlobalContext.BUNDLE_ENDDATE, ""));
			et_post.setText(bundle.getString(GlobalContext.BUNDLE_POST, ""));
			et_desc.setText(bundle.getString(GlobalContext.BUNDLE_CONTENT,""));
		}
		
	}
	private void init() {
		rl_partIn = (RelativeLayout) findViewById(R.id.edit_work_inTime);
		rl_deTime = (RelativeLayout) findViewById(R.id.edit_work_deTime);
		tv_partIn = (TextView) findViewById(R.id.tv_partIn_time);
		tv_deTime = (TextView) findViewById(R.id.tv_departure_time);
		saveBtn = (Button) findViewById(R.id.job_experience_save);
		et_post = (EditText) findViewById(R.id.add_post_name);
		et_desc = (EditText) findViewById(R.id.add_work_content);
		rl_partIn.setOnClickListener(this);
		rl_deTime.setOnClickListener(this);
		saveBtn.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		super.onClick(v);
		switch (v.getId()) {
		case R.id.edit_work_inTime:	// 日期格式为yyyy-MM-dd HH:mm
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.CHINA);  
		    String now = sdf.format(new Date());  
			if(TextUtils.isEmpty(tv_partIn.getText().toString())){
				customDatePicker1.show(now);
			}else{
				customDatePicker1.show(tv_partIn.getText().toString());
			}
			break;
		case R.id.edit_work_deTime:
			SimpleDateFormat de_sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.CHINA);  
		    String deNow= de_sdf.format(new Date());  
			if(TextUtils.isEmpty(tv_deTime.getText().toString())){
				customDatePicker2.show(deNow);
			}else{
				customDatePicker2.show(tv_deTime.getText().toString());
			}
			break;
		case R.id.job_experience_save:
			String start_time = tv_partIn.getText().toString().trim();
			String de_time = tv_deTime.getText().toString().trim();
			String post = et_post.getText().toString().trim();
			String desc = et_desc.getText().toString().trim();
			if (TextUtils.isEmpty(start_time)) {
				Notification.toast(this, "选择入职时间");
				return;
			}
			if (TextUtils.isEmpty(de_time)) {
				Notification.toast(this, "选择离职时间");
				return;
			}
			if (TextUtils.isEmpty(post)) {
				Notification.toast(this, "选择岗位名称");
				return;
			}
			if (TextUtils.isEmpty(desc)) {
				Notification.toast(this, "工作内容不能为空");
				return;
			}
			
			if (!Utils.IsEndAfterStart(start_time, de_time)) {
				Notification.toast(this, "入职时间不能大于离职时间");
				return;
			}
			
			if (!isClick) {
				showWaittingDialog();
				Integer _ID = null;
				if(null == id || 0 == id){
					_ID = null;
				}else{
					_ID  = id;
				}
				manager.jobWorkAdd(_ID, GlobalContext.accountId, start_time, de_time, post, desc);
				isClick =  true;
			}
			break;
		default:
			break;
		}
	}
	
	private void showWaittingDialog(){
		pDialog = LoadingDialog.createLoadingDialog(this, "正在提交...");
		pDialog.show();
	}
	
	private void initDatePicker() {  
	    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.CHINA);  
	    String now = sdf.format(new Date());  
	    customDatePicker1 = new CustomDatePicker(this, new CustomDatePicker.ResultHandler() {  
	        @Override  
	        public void handle(String time) { // 回调接口，获得选中的时间  
	        	tv_partIn.setText(time.split(" ")[0]);  
	        }  
	    }, "1980-01-01 00:00", now); // 初始化日期格式请用：yyyy-MM-dd HH:mm，否则不能正常运行  
	    customDatePicker1.showSpecificTime(false); // 显示时和分  
	    customDatePicker1.setIsLoop(true); // 允许循环滚动  
	  
	    customDatePicker2 = new CustomDatePicker(this, new CustomDatePicker.ResultHandler() {  
	        @Override  
	        public void handle(String time) { // 回调接口，获得选中的时间  
	        	tv_deTime.setText(time.split(" ")[0]);  
	        }  
	    }, "1980-01-01 00:00", now); // 初始化日期格式请用：yyyy-MM-dd HH:mm，否则不能正常运行  
	    customDatePicker2.showSpecificTime(false); // 显示时和分  
	    customDatePicker2.setIsLoop(true); // 允许循环滚动  
	}

	@Override
	public void onJobWorkAddFailure(int errorCode, String errorMsg) {
		if(null != pDialog && pDialog.isShowing()){
			pDialog.dismiss();
		}
		isClick = false;
	}

	@Override
	public void onJobWorkAddSuccess(WorkExperienceData data) {
		if(null != pDialog && pDialog.isShowing()){
			pDialog.dismiss();
		}
		isClick = false;
		if(null == id || 0 == id){
			WorkListChangeManager.getInstance().notifyAdd(data);
		}else{
			WorkListChangeManager.getInstance().notifyModify(id, data);
		}
		finish();
	}  

	public void onResume() {
		super.onResume();
		MobclickAgent.onResume(this);
	}

	public void onPause() {
		super.onPause();
		MobclickAgent.onPause(this);
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		if(null != pDialog && pDialog.isShowing()){
			pDialog.dismiss();
		}
	}
	
}
